package com.ktjy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pages.ProjectPage;
import com.ktjy.pojo.SalChance;
import com.ktjy.pojo.SalPlan;

import com.ktjy.pojo.vo.SysUserVo;
import com.ktjy.service.SalChanceService;
import com.ktjy.service.SalPlanService;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 客户开发计划
 */
@Controller
public class planController {
    @Autowired
    SalChanceService salChanceService;
    @Autowired
    SalPlanService salPlanService;

    @RequestMapping("/plan/list")
    public Object PlanList(SalChance salChance, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "3") Integer pageSize, Model model) {
        //获取当前登录人的id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().
                like(salChance.getChcCustName() != null, SalChance::getChcCustName, salChance.getChcCustName())
                .like(salChance.getChcDueTo() != null, SalChance::getChcDueTo, salChance.getChcDueTo())
                .like(salChance.getChcTitle() != null, SalChance::getChcTitle, salChance.getChcTitle())
                .eq(SalChance::getChcDueId, currentUser.getUsrId());
        IPage<SalChance> chanceIPage = new Page<>(pageIndex, pageSize);
        Page<SalChance> page = (Page<SalChance>) salChanceService.page(chanceIPage, queryWrapper);
        ProjectPage<SalChance> projectPage = new ProjectPage<>();
        BeanUtils.copyProperties(page, projectPage);
        model.addAttribute("planPage", projectPage);
        return "/plan/list";
    }

    @RequestMapping("/plan/add")
    public Object planAdd(Model model) {
        //获取当前登录人的id
        SysUserVo currentUser = (SysUserVo) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SalChance> queryWrapper = new QueryWrapper<>();
        if (currentUser.getRole().getRoleId()==2){
            queryWrapper.lambda().eq(SalChance::getChcDueId,currentUser.getUsrId());
        }
        model.addAttribute("users", salChanceService.list(queryWrapper));
        return "/plan/add";
    }

    @RequestMapping("/plan/save")
    public Object planSave(SalPlan salPlan,Model model) {
        boolean result = salPlanService.saveOrUpdate(salPlan);
        if (result) {
            return "redirect:/plan/list";
        } else if (salPlan.getPlaId() == 0) {
            return "redirect:/plan/add";
        } else {
            return "redirect:/plan/edit?usrId=" + salPlan.getPlaId();
        }
    }
}
